package com.h3c.web.rest;

import java.util.Collection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections4.Predicate;
import org.jasig.cas.CentralAuthenticationService;
import org.jasig.cas.ticket.Ticket;
import org.jasig.cas.ticket.TicketGrantingTicket;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.h3c.web.common.BaseResponse;
import com.h3c.web.common.utils.SpringContextUtil;
import com.h3c.web.common.vo.LoginInfoVO;
import com.h3c.web.support.cas.common.ClientInfoHolder;

/**
 * @ClassName: MonitorRest
 * @Description: 监控restapi
 * @author: d14287
 * @date: 2017年5月31日 下午3:43:50
 */
@Controller
@RequestMapping("console/monitor")
public class MonitorRest {
	/**
	 * @Desc: 查询当前在线用户列表
	 * @param request
	 * @return
	 * @return: BaseResponse
	 */
	@RequestMapping(value = "/listOnline.ajax", method = RequestMethod.GET)
	public @ResponseBody
	BaseResponse queryOnline(HttpServletRequest request) {
		CentralAuthenticationService centralAuthenticationService = SpringContextUtil
				.getBean("centralAuthenticationService");
		Collection<Ticket> tickets = centralAuthenticationService
				.getTickets(new Predicate<Object>() {
					@Override
					public boolean evaluate(final Object ticket) {
						if (ticket instanceof TicketGrantingTicket) {
							return !((TicketGrantingTicket) ticket).isExpired();
						}
						return false;
					}
				});
		List<LoginInfoVO> loginInfoList = Lists.newArrayList();
		for (final Ticket ticket : tickets) {
			final TicketGrantingTicket tgt = (TicketGrantingTicket) ticket;
			LoginInfoVO loginInfo = new LoginInfoVO();
			loginInfo
					.setLoginId(tgt.getAuthentication().getPrincipal().getId());
			loginInfo.setLoginTime(tgt.getAuthentication()
					.getAuthenticationDate());
			loginInfo.setTgt(tgt.getId());
			loginInfo.setClientIp(ClientInfoHolder.get(tgt.getId())
					.getClientIp());
			loginInfo.setServerIp(ClientInfoHolder.get(tgt.getId())
					.getLocalIp());
			loginInfoList.add(loginInfo);
		}
		return BaseResponse.trueOf(loginInfoList);
	}

	/**
	 * @Desc: 根据用户名查询用户是否登录
	 * @param loginId
	 * @param request
	 * @return
	 * @return: BaseResponse
	 */
	@RequestMapping(value = "/queryByLoginId.ajax", method = RequestMethod.GET)
	public @ResponseBody
	BaseResponse queryByLoginId(@RequestParam("loginId") String loginId,
			HttpServletRequest request) {
		CentralAuthenticationService centralAuthenticationService = SpringContextUtil
				.getBean("centralAuthenticationService");
		Collection<Ticket> tickets = centralAuthenticationService
				.getTickets(new Predicate<Object>() {
					@Override
					public boolean evaluate(final Object ticket) {
						if (ticket instanceof TicketGrantingTicket) {
							return !((TicketGrantingTicket) ticket).isExpired();
						}
						return false;
					}
				});
		List<LoginInfoVO> loginInfoList = Lists.newArrayList();
		for (final Ticket ticket : tickets) {
			final TicketGrantingTicket tgt = (TicketGrantingTicket) ticket;
			if (loginId.equals(tgt.getAuthentication().getPrincipal().getId())) {
				LoginInfoVO loginInfo = new LoginInfoVO();
				loginInfo.setLoginId(tgt.getAuthentication().getPrincipal()
						.getId());
				loginInfo.setLoginTime(tgt.getAuthentication()
						.getAuthenticationDate());
				loginInfo.setTgt(tgt.getId());
				loginInfo.setClientIp(ClientInfoHolder.get(tgt.getId())
						.getClientIp());
				loginInfo.setServerIp(ClientInfoHolder.get(tgt.getId())
						.getLocalIp());
				loginInfoList.add(loginInfo);
			}
		}
		return BaseResponse.trueOf(loginInfoList);
	}

	/**
	 * @Desc: 剔除登录用户
	 * @param tgt
	 * @param request
	 * @return
	 * @return: BaseResponse
	 */
	@RequestMapping(value = "/enoffline.ajax", method = RequestMethod.POST)
	public @ResponseBody
	BaseResponse enOffLine(@RequestParam("tgt") String tgt,
			HttpServletRequest request) {
		CentralAuthenticationService centralAuthenticationService = SpringContextUtil
				.getBean("centralAuthenticationService");
		centralAuthenticationService.destroyTicketGrantingTicket(tgt);
		return BaseResponse.trueOf();
	}
}
